package org.jeecg.modules.solitaire_app.app.controller;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.solitaire_app.app.entity.VO.OilDatasVO;
import org.jeecg.modules.solitaire_app.app.entity.YsyOilRawMaterial;
import org.jeecg.modules.solitaire_app.app.entity.YsyOilRawMaterialPrice;
import org.jeecg.modules.solitaire_app.app.entity.YsyRowMaterialRefinery;
import org.jeecg.modules.solitaire_app.app.mapper.YsyOilRawMaterialMapper;
import org.jeecg.modules.solitaire_app.app.service.IYsyOilRawMaterialPriceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.solitaire_app.app.service.IYsyOilRawMaterialService;
import org.jeecg.modules.solitaire_app.app.service.IYsyRowMaterialRefineryService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 油原料
 * @Author: jeecg-boot
 * @Date: 2023-10-16
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "油原料")
@RestController
@RequestMapping("/app/ysyOilRawMaterialPrice")
public class YsyOilRawMaterialPriceController extends JeecgController<YsyOilRawMaterialPrice, IYsyOilRawMaterialPriceService> {
    @Autowired
    private IYsyOilRawMaterialPriceService ysyOilRawMaterialPriceService;
    @Autowired
    private IYsyOilRawMaterialService ysyOilRawMaterialService;

    /**
     * 分页列表查询
     *
     * @param ysyOilRawMaterialPrice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "油原料-分页列表查询")
    @ApiOperation(value = "油原料-分页列表查询", notes = "油原料-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YsyOilRawMaterialPrice ysyOilRawMaterialPrice,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YsyOilRawMaterialPrice> queryWrapper = QueryGenerator.initQueryWrapper(ysyOilRawMaterialPrice, req.getParameterMap());
        Page<YsyOilRawMaterialPrice> page = new Page<YsyOilRawMaterialPrice>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
        IPage<YsyOilRawMaterialPrice> pageList = ysyOilRawMaterialPriceService.page(page, queryWrapper);

        pageList.getRecords().stream().forEach(item -> {
            YsyOilRawMaterial ysyOilRawMaterial = ysyOilRawMaterialService.getOne(Wrappers.<YsyOilRawMaterial>lambdaQuery()
                    .eq(YsyOilRawMaterial::getOilRawMaterialId, item.getOilRawMaterialId()));
            item.setOilRawMaterialName(ysyOilRawMaterial.getRawMaterialName());
        });

        return Result.ok(pageList);
    }

    /**
     * 走势图
     *
     * @param map
     * @return
     */
    @AutoLog(value = "油原料-走势图")
    @ApiOperation(value = "油原料-走势图", notes = "油原料-走势图")
    @PostMapping(value = "/getOilDatasVO")
    public Result<List<OilDatasVO>> getOilDatasVO(@RequestBody Map<String, Object> map) {
        return ysyOilRawMaterialPriceService.getOilDatasVOs(map);
    }

    /**
     * 最新成品油价
     *
     * @param map
     * @return
     */
    @AutoLog(value = "油原料-最新成品油价")
    @ApiOperation(value = "油原料-最新成品油价", notes = "油原料-最新成品油价")
    @PostMapping(value = "/getOilNewData")
    public Result<OilDatasVO> getOilNewData(@RequestBody Map<String, Object> map) {
        return ysyOilRawMaterialPriceService.getOilNewData(map);
    }

    /**
     * 添加
     *
     * @param ysyOilRawMaterialPrice
     * @return
     */
    @AutoLog(value = "油原料-添加")
    @ApiOperation(value = "油原料-添加", notes = "油原料-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YsyOilRawMaterialPrice ysyOilRawMaterialPrice) {
        ysyOilRawMaterialPriceService.save(ysyOilRawMaterialPrice);
        return Result.ok("添加成功！");
    }

    /**
     * 添加
     *
     * @param ysyOilRawMaterialPrices
     * @return
     */
    @AutoLog(value = "油原料-添加")
    @ApiOperation(value = "油原料-添加", notes = "油原料-添加")
    @PostMapping(value = "/bathAdd")
    public Result<?> bathAdd(@RequestBody List<YsyOilRawMaterialPrice> ysyOilRawMaterialPrices) {
        ysyOilRawMaterialPrices.stream().forEach(item -> {

            //查询上一条填报数据
            YsyOilRawMaterialPrice ysyOilRawMaterialPrice = ysyOilRawMaterialPriceService.getOneOilPrice(item.getOilRawMaterialId(),item.getRefineryId());


            YsyOilRawMaterialPrice ysyOilRawMaterialPriceDB = new YsyOilRawMaterialPrice();

            ysyOilRawMaterialPriceDB = item;

            if (item.getListPrice().compareTo(new BigDecimal(0)) == 0){
                ysyOilRawMaterialPriceDB.setListPrice(ysyOilRawMaterialPrice==null?new BigDecimal(0):ysyOilRawMaterialPrice.getListPrice());
            }

            if (item.getPreferentialPrice().compareTo(new BigDecimal(0)) == 0){
                ysyOilRawMaterialPriceDB.setPreferentialPrice(ysyOilRawMaterialPrice==null?new BigDecimal(0):ysyOilRawMaterialPrice.getPreferentialPrice());
            }

            ysyOilRawMaterialPriceDB.setCreateTime(new Date());
            ysyOilRawMaterialPriceService.save(ysyOilRawMaterialPriceDB);
        });
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ysyOilRawMaterialPrice
     * @return
     */
    @AutoLog(value = "油原料-编辑")
    @ApiOperation(value = "油原料-编辑", notes = "油原料-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YsyOilRawMaterialPrice ysyOilRawMaterialPrice) {
        ysyOilRawMaterialPriceService.updateById(ysyOilRawMaterialPrice);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "油原料-通过id删除")
    @ApiOperation(value = "油原料-通过id删除", notes = "油原料-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        ysyOilRawMaterialPriceService.remove(Wrappers.<YsyOilRawMaterialPrice>lambdaQuery().eq(YsyOilRawMaterialPrice::getId, id));
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "油原料-批量删除")
    @ApiOperation(value = "油原料-批量删除", notes = "油原料-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ysyOilRawMaterialPriceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "油原料-通过id查询")
    @ApiOperation(value = "油原料-通过id查询", notes = "油原料-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YsyOilRawMaterialPrice ysyOilRawMaterialPrice = ysyOilRawMaterialPriceService.getById(id);
        return Result.ok(ysyOilRawMaterialPrice);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ysyOilRawMaterialPrice
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YsyOilRawMaterialPrice ysyOilRawMaterialPrice) {
        return super.exportXls(request, ysyOilRawMaterialPrice, YsyOilRawMaterialPrice.class, "油原料");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YsyOilRawMaterialPrice.class);
    }

}
